require 'active_support/multibyte/unicode'
require 'cocoapods/config'

class Flutter

    def initialize()

    end

    def analyzer
        project_root = Pod::Config.new().installation_root
        podfile_path = File.join(project_root, "Podfile")
        file = File.open(podfile_path, "r")
        is_flutter = false
        flutter_plugin_registrant_path = nil
        file.each_line do |line|
            _line = line.lstrip
            if _line.start_with?("flutter_application_path") 
                is_flutter = true
                puts "project contain flutter..."
            end
            if _line.index("FlutterPluginRegistrant") != nil
                array = _line.split("\'")
                if array != nil && array.size > 2
                    flutter_plugin_registrant_path = array[3]
                end 
            end
        end
        file.close
        
        if is_flutter && flutter_plugin_registrant_path != nil
            flutter_plugin_registrant_absolute_path = File.join(project_root, flutter_plugin_registrant_path)
            podspec = File.join(flutter_plugin_registrant_absolute_path, "FlutterPluginRegistrant.podspec")
            implementation = File.join(flutter_plugin_registrant_absolute_path, "Classes" , "GeneratedPluginRegistrant.m")
            
            if File.exist?(podspec) == false || File.exist?(implementation) == false
                return
            end

            # begin 
            file = File.open(podspec, "r")
            is_native_app_bridging = false
            podspec_string = ""
            file.each_line do |line|
                if line.index("native_app_bridging") != nil
                    is_native_app_bridging = true
                else
                    podspec_string += line
                end
            end
            file.close unless file == nil

            if is_native_app_bridging && podspec_string.length > 0  
                File.write(podspec, podspec_string)
                puts "已删除 native_app_bridging 库!"
            end

            file = File.open(implementation, "r")
            is_native_app_bridging = false
            implementation_string = ""
            native_app_bridging_index = 0
            
            file.each_line do |line|
                if is_native_app_bridging 
                    native_app_bridging_index += 1
                end
                if line.index("native_app_bridging") != nil && !is_native_app_bridging
                    is_native_app_bridging = true
                else
                    if native_app_bridging_index == 0 || native_app_bridging_index > 5
                       if line.index("NativeAppBridgingPlugin") == nil
                            implementation_string += line
                        end     
                    end
                end
            end
            file.close unless file == nil

            # puts "GeneratedPluginRegistrant修改后：#{implementation_string}"
            if is_native_app_bridging && implementation_string.length > 0       
                File.write(implementation, implementation_string)
            end
           
        end
        # puts "anslyzer......"1
        
    end
end